<?php

namespace app\service;

use app\model\SystemDynamicFrom;
use app\model\SystemDynamicFromField;
use app\model\SystemFieldPower;
use app\model\SystemMenu;
use app\model\SystemMenuDataPower;
use app\stateData;
use stdClass;
use think\facade\Cache;

class Power
{
    /**
     * 查询职位id对应菜单
     */
    public function getPersonList($level_id,$menu,$system): array
    {
        $menuModel = new SystemMenu();
        //查询菜单列表
        $result['menuList'] = $menuModel->getMenuList();

        //获取level_id对应的菜单与可见范围权限
        $data = $menu->getPositionDataDetails($level_id);

        //数据可见范围
        if (empty($data)){
            $result['dataPower'] = [];
        }else{
            $result['dataPower'] = $data['data_range'];
        }

        if (!empty($data['menu_alias_id'])){
            $result['menuList'] = $system->menuTreeList ($system->powerPoint(
                $result['menuList'],
                $system->analysisId ($data['menu_alias_id']),
                'alias_id',
                'menu_alias_id'
            ));
        }else{
            $result['menuList'] = $system->menuTreeList ($system->powerPoint($result['menuList'],''));
        }

        return $result;
    }

    /**
     * 查询用户数据范围
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserDataRange($uid): array
    {

        $user = new \app\model\User();
        $dataPower = new SystemMenuDataPower();
        $userService = new User();
        $uData = $user->getUserLevelInfoModel($uid);

        if (is_array ($uData['level_id'])){
            $level = $uData['level_id']['id'];
        }else{
            $level = $uData['level_id'];
        }
        $data_range = $dataPower->getPositionDataRangeModel($level);
        //如果登陆账号为admin
        if($uid === stateData::$_admin['id']){
            $data_range = stateData::$_admin['id'];
        }

        if (empty($data_range)){
            $data_range = 4;
        }

        $where = $userService->getWhere ($data_range,$uData);

        //根据条件查询用户范围
        return $user->getFIdResult($where);
    }

    /**
     * 获取当前用户数据可见范围
     */

    public function getUserDataRand($uid)
    {
        $user = new \app\model\User();
        $dataPower = new SystemMenuDataPower();

        //先查询用户职位信息
        $uData = $user->getUidLevelIdModel($uid);

        //通过职位信息查询该职位对应的数据可见范围
        $dataRange = $dataPower->getPositionDataRangeModel($uData);

        if (empty($dataRange)){
            $dataRange = 4;
        }

        return $dataRange;

    }

    /**
     * 获取当前用户所有字段
     * @param $aliasId
     * @param bool $type //true 删除系统字段
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserFieldPower($aliasId,$uid, bool $type=false): array
    {

        $from = new SystemDynamicFrom();
        $userService = new User();
        $system = new System();
        $user = new \app\model\User();
        $systemDynamicFromField = new SystemDynamicFromField();
        $menuModel = new SystemMenu();

        $fieldListTree = [];

        if (!empty($aliasId)){
            //通过菜单id找模板id
            $fid = $from->getFromId($aliasId);
            //获取用户字段权限
            $fieldData = $userService->userFieldPower ($user,$fid,$uid);

            //获取模板字段详情
            $fieldList = $systemDynamicFromField->getTmpFieldList($fid);
            //去除多余权限
            $fieldListTree = $system->powerPoint($fieldList,array_values($fieldData));
        }


        if ($type){
            foreach ($fieldListTree as $key=>$item) {
                if ($item['attribute'] === '系统字段'){
                    unset($fieldListTree[$key]);
                }
            }
        }

        foreach ($fieldListTree as &$item) {
            if (!empty($item['type_value'])){

                $item['type_value'] = json_decode ($item['type_value'], false);
            }
        }
        return $fieldListTree;
    }



    public function getBUttonPower($aliasId,$uid)
    {

        $user = new \app\model\User();
        $menuPower = new SystemMenuDataPower();
        $menu = new SystemMenu();
        $system = new System();

        //获取用户权限
        $userInfo = $user->getIdUserData($uid);

        //通过用户绑定职位获取菜单级可见范围
        $userMenuPower = $menuPower->getPositionDataDetails ($userInfo['level_id']);

        //获取菜单权限
        $id = $menu->getAliasIdMenu($aliasId);

        $ops = $menu->getAliasIdSonDataModel($id);

        if (!empty($ops)){
            $data = $system->powerPoint(
                    $ops,
                    $system->analysisId ($userMenuPower['menu_alias_id']),
                    'alias_id',
                    'menu_alias_id');
        }else{
            $data = [];
        }
        return $data;
    }



}